﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace 随机点名
{
    /// <summary>
    /// StuName.xaml 的交互逻辑
    /// </summary>
    public partial class StuName : Window
    {
        Thread tr;
        delegate void ShowInLabel(string s);
        ShowInLabel showInLabel;
        public bool choosed=false;
        MainWindow w;
        public StuName(MainWindow m)
        {
            InitializeComponent();
            showInLabel = new ShowInLabel(ShowLabel);
            this.Left = SystemParameters.WorkArea.Size.Width/2-210;
            this.Top = SystemParameters.WorkArea.Size.Height/2-90;
            w = m;
        }

        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (w.btnRandom.Content.ToString() == "随机点名")
            {
                this.Close();
            }
        }
        void ShowLabel(string s)
        {
            lbStu.Content = s;
        }
        private void showINLabel()
        {
            int num = Student.StuName.Count();
            Random rd = new Random();
            while (!choosed)
            {
                lbStu.Dispatcher.Invoke(showInLabel, Student.StuName[rd.Next(1, num)]);
                Thread.Sleep(100);
            }
            Thread.CurrentThread.Abort();
        }

        private void lbStu_Loaded(object sender, RoutedEventArgs e)
        {
            tr = new Thread(showINLabel);
            tr.Start();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            tr.Abort();
        }
    }
}
